package com.sanpu.kxe.business.main.fragment;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.library.imagechoose.PhotoPicker;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseFragment;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.CertificationOldOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.contract.OldAuthSecondStepContract;
import com.sanpu.kxe.business.main.interfaces.IOnSwitchPageListener;
import com.sanpu.kxe.business.main.presenter.OldAuthSecondStepPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.business.main.view.OldCertificateSecondActivity;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.eventbus.ReviewPhotoEvent;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.rxservice.RequestMap;
import com.sanpu.kxe.utils.ToastUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：人工审核第二步
 * Created by 薛勇军 on 2019/5/15.
 */

public class OldAuthSecondStepFragment extends BaseFragment implements OldAuthSecondStepContract.View {

    @Bind(R.id.front)
    ImageView vFront;
    @Bind(R.id.front_image)
    ImageView vFrontImage;
    @Bind(R.id.front_image_cloud)
    TextView vFrontImageCloud;
    @Bind(R.id.reverse)
    ImageView vReverse;
    @Bind(R.id.reverse_image)
    ImageView vReverseImage;
    @Bind(R.id.reverse_image_cloud)
    TextView vReverseImageCloud;
    @Bind(R.id.commit)
    RoundTextView vCommit;
    @Bind(R.id.lastStepRtv)
    RoundTextView lastStepRtv;

    private OldAuthSecondStepContract.Presenter mPresenter;

    public IOnSwitchPageListener mOnSwitchPageListener;

    public void setOnSwitchPageListener(IOnSwitchPageListener mOnSwitchPageListener) {
        this.mOnSwitchPageListener = mOnSwitchPageListener;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_auth_second_old;
    }

    @Override
    protected void initView() {
        super.initView();
        mPresenter = new OldAuthSecondStepPresenter(this);
        CertificationOldOutput mOutput = (CertificationOldOutput)
                getArguments().getSerializable(MainConfig.INTENT_CERTIFICATION_OLD_INPUT);
        boolean isComeFromNews = getArguments().getBoolean(MainConfig.INTENT_COME_FROM_NEWS);
        if (isComeFromNews) {
            HashMap<String, File> mUploadFiles = ((OldCertificateSecondActivity) getActivity()).getmUploadFiles();
            if (mUploadFiles == null)
                mUploadFiles = new HashMap<>();
            mPresenter.setImgFiles(mUploadFiles);
            parseAndDisplayImage(mUploadFiles.get(CommonConfig.TYPE_IDFROPIC), vFrontImage, vFrontImageCloud);
            parseAndDisplayImage(mUploadFiles.get(CommonConfig.TYPE_IDCONPIC), vReverseImage, vReverseImageCloud);
        } else if (mOutput != null && !TextUtils.equals(CommonConfig.AUTHEN_NOTSUB, mOutput.getChkSts())) {
            ((BaseTitleActivity) getActivity()).showLoadingDialog(false, "正在加载图片，请稍后...");
            parseAndDisplayImage(mOutput.getIdFroPic(), vFrontImage, vFrontImageCloud,
                    CommonConfig.TYPE_IDFROPIC);
            parseAndDisplayImage(mOutput.getIdConPic(), vReverseImage, vReverseImageCloud,
                    CommonConfig.TYPE_IDCONPIC);
        }
    }

    /**
     * 照片回显
     */
    public void parseAndDisplayImage(String imageUrl, ImageView imageView, TextView textView, String fileName) {
        if (!TextUtils.isEmpty(imageUrl)) {
            String imgUrl = UrlConstants.getBaseUrl() + imageUrl + RequestMap.sessionIdReqStr();
            imageView.setTag(imgUrl);
            JKImageLoader.getInstance().displayImage(getContext(), imgUrl, imageView);
            textView.setVisibility(View.VISIBLE);
            mPresenter.getImageFileByNet(imgUrl, fileName);
        }
    }

    /**
     * 照片回显
     */
    public void parseAndDisplayImage(File imageFile, ImageView imageView, TextView textView) {
        if (imageFile != null && imageFile.exists()) {
            imageView.setTag(imageFile.getPath());
            JKImageLoader.getInstance().displayImage(getContext(), imageFile, imageView);
            textView.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.commit, R.id.lastStepRtv, R.id.front, R.id.reverse})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.commit:
                if (vFrontImage.getTag() == null || vReverseImage.getTag() == null) {
                    ToastUtil.showTextViewPrompt("必须添加身份证正反面照片");
                    return;
                }
                ((OldCertificateSecondActivity) getActivity()).setUploadFiles(mPresenter.getFiles());
                mOnSwitchPageListener.onSwitchPage();
                break;
            case R.id.lastStepRtv:
                getActivity().finish();
                break;
            case R.id.front:
                if (vFrontImage.getTag() != null) {
                    mPresenter.takePhoto((String) vFrontImage.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_FRONT);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_IDFROPIC);
                }
                break;
            case R.id.reverse:
                if (vReverseImage.getTag() != null) {
                    mPresenter.takePhoto((String) vReverseImage.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_REVERSE);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_IDCONPIC);
                }
                break;
        }
    }

    @Override
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && data != null) {
            ArrayList<String> photos =
                    data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            switch (requestCode) {
                case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_FRONT:
                    if (photos == null || photos.size() == 0) {
                        vFrontImage.setTag(null);
                        vFrontImage.setImageDrawable(null);
                        vFrontImage.setVisibility(View.GONE);
                        vFrontImageCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_IDFROPIC);
                    }
                    break;
                case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_REVERSE:
                    if (photos == null || photos.size() == 0) {
                        vReverseImage.setTag(null);
                        vReverseImage.setImageDrawable(null);
                        vReverseImage.setVisibility(View.GONE);
                        vReverseImageCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_IDCONPIC);
                    }
                    break;
            }
        }
    }

    @Override
    public void refreshView(ReviewPhotoEvent event) {
        String filePath = event.getFilePath();
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        String fileIdName;
        switch (event.getCurrentType()) {
            case CommonConfig.TYPE_IDFROPIC:
                fileIdName = CommonConfig.TYPE_IDFROPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vFrontImage);
                vFrontImage.setTag(filePath);
                vFrontImage.setVisibility(View.VISIBLE);
                vFrontImageCloud.setVisibility(View.VISIBLE);
                break;
            case CommonConfig.TYPE_IDCONPIC:
                fileIdName = CommonConfig.TYPE_IDCONPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vReverseImage);
                vReverseImage.setTag(filePath);
                vReverseImage.setVisibility(View.VISIBLE);
                vReverseImageCloud.setVisibility(View.VISIBLE);
                break;
            default:
                return;
        }
        mPresenter.onCompress(filePath, fileIdName);
    }

    @Override
    public void dismissLoadingDialog() {
        ((BaseTitleActivity) getActivity()).dismissLoadingDialog();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(OldAuthSecondStepContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
